package org.mat.ipaas.ucenter.service.modules.oss.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.mat.framework.lang.dto.MatApiResponse;
import org.mat.ipaas.ucenter.biz.common.system.query.QueryGenerator;
import org.mat.ipaas.ucenter.biz.modules.oss.entity.OssFile;
import org.mat.ipaas.ucenter.biz.modules.oss.service.IOssFileService;
import org.mat.ipaas.ucenter.service.api.oss.OssFileRestAPI;
import org.mat.ipaas.ucenter.service.dto.oss.entity.OssFileDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 云存储示例 DEMO
 *
 */
@Slf4j
@Controller
@RequestMapping("/sys/oss/file")
public class OssFileController implements OssFileRestAPI {

    @Autowired
    private IOssFileService ossFileService;

    @ResponseBody
    @GetMapping("/list")
    public MatApiResponse<IPage<OssFileDTO>> queryPageList(OssFileDTO ossFileDTO,
                                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        MatApiResponse<IPage<OssFileDTO>> matApiResponse = new MatApiResponse<>();
        OssFile file = toOssFile(ossFileDTO);

        QueryWrapper<OssFile> queryWrapper = QueryGenerator.initQueryWrapper(file, req.getParameterMap());
        Page<OssFile> page = new Page<>(pageNo, pageSize);
        IPage<OssFile> pageList = ossFileService.page(page, queryWrapper);
        matApiResponse.setSuccess(true);
        matApiResponse.setData(toOssFileDTOPage(pageList));
        return matApiResponse;
    }

    @ResponseBody
    @PostMapping("/upload")
    //@RequiresRoles("admin")
    //@RequiresPermissions("system:ossFile:upload")
    public MatApiResponse upload(@RequestParam("file") MultipartFile multipartFile) {
        MatApiResponse matApiResponse = new MatApiResponse();
        try {
            ossFileService.upload(multipartFile);
            matApiResponse.success("上传成功！");
        } catch (Exception ex) {
            log.info(ex.getMessage(), ex);
            matApiResponse.fail("上传失败");
        }
        return matApiResponse;
    }

    @ResponseBody
    @DeleteMapping("/delete")
    public MatApiResponse delete(@RequestParam(name = "id") String id) {
        MatApiResponse matApiResponse = new MatApiResponse();
        OssFile file = ossFileService.getById(id);
        if (file == null) {
            matApiResponse.fail("未找到对应实体");
        } else {
            boolean ok = ossFileService.delete(file);
            matApiResponse.success("删除成功!");
        }
        return matApiResponse;
    }

    /**
     * 通过id查询.
     */
    @ResponseBody
    @GetMapping("/queryById")
    public MatApiResponse<OssFileDTO> queryById(@RequestParam(name = "id") String id) {
        MatApiResponse<OssFileDTO> matApiResponse = new MatApiResponse<>();
        OssFile file = ossFileService.getById(id);
        if (file == null) {
            matApiResponse.fail("未找到对应实体");
        } else {
            matApiResponse.setData(toOssFileDTO(file));
            matApiResponse.setSuccess(true);
        }
        return matApiResponse;
    }


    private Page<OssFileDTO> toOssFileDTOPage(IPage<OssFile> pageList) {
        Page<OssFileDTO> ossFileDTOPage = new Page<>();
        List<OssFileDTO> ossFileDTOList = new ArrayList<>();
        if (pageList != null && CollectionUtils.isNotEmpty(pageList.getRecords())) {
            for (OssFile ossFile : pageList.getRecords()) {
                ossFileDTOList.add(toOssFileDTO(ossFile));
            }
        }

        ossFileDTOPage.setTotal(pageList.getTotal());
        ossFileDTOPage.setSize(pageList.getSize());
        ossFileDTOPage.setCurrent(pageList.getCurrent());
        ossFileDTOPage.setRecords(ossFileDTOList);
        return ossFileDTOPage;
    }

    private OssFileDTO toOssFileDTO(OssFile ossFile) {
        if (ossFile == null) {
            return null;
        }
        OssFileDTO ossFileDTO = new OssFileDTO();
        ossFileDTO.setFileName(ossFile.getFileName());
        ossFileDTO.setUrl(ossFile.getUrl());
        ossFileDTO.setId(ossFile.getId());
        ossFileDTO.setCreateBy(ossFile.getCreateBy());
        ossFileDTO.setCreateTime(ossFile.getCreateTime());
        ossFileDTO.setUpdateBy(ossFile.getUpdateBy());
        ossFileDTO.setUpdateTime(ossFile.getUpdateTime());
        return ossFileDTO;
    }

    private OssFile toOssFile(OssFileDTO ossFileDTO) {
        if (ossFileDTO == null) {
            return null;
        }
        OssFile ossFile = new OssFile();
        ossFile.setId(ossFileDTO.getId());
        ossFile.setCreateBy(ossFileDTO.getCreateBy());
        ossFile.setCreateTime(ossFileDTO.getCreateTime());
        ossFile.setUpdateBy(ossFileDTO.getUpdateBy());
        ossFile.setUpdateTime(ossFileDTO.getUpdateTime());
        ossFile.setFileName(ossFileDTO.getFileName());
        ossFile.setUrl(ossFileDTO.getUrl());
        return ossFile;
    }
}
